<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        session_start("login");
    }

    public function indexAction()
    {
        /* DB Connectors */
        $dbCompany = new Application_Model_DbTable_Company();
        $dbEvaluation = new Application_Model_DbTable_Evaluation();
        $dbCategory = new Application_Model_DbTable_Category();
        
        /* Busca as categorias */
        $categoriesResult = $dbCategory->fetchAll(
            $dbCategory->select('id, name')
        );
        foreach ($categoriesResult as $key => $value) {
            $categories[$value['id']] = $value['name'];
        }
        $this->view->categories = $categories;
        
        /* Busca as empresas */
        $companiesResult = $dbCompany->fetchAll(
                $dbCompany->select()
                          ->where('active = ?', '1')
                          ->where('deleted = ?', '0')
                          ->order('RAND()')
                          ->limit(4, 0)
        );
        
        /* Constroi o array de empresas  */
        $companies = array();
        foreach ($companiesResult as $company) {
            $companyItem['name'] = $company['name'];
            $companyItem['logo'] = $company['logo'];
            $companyItem['friendly_name'] = $company['friendly_name'];
            $companyItem['category'] = $categories[$company['category_id']];
            /* Carrega Avaliações */
            $evals = $dbEvaluation->fetchAll(
                $dbEvaluation->select()
                      ->where('active = ?', '1')
                      ->where('deleted = ?', '0')
                      ->where('company_id = ?', $company['id'])
            );
            
            /* Quantidade Avaliações */
            $companyItem['evaluations'] = $evals->count();
            
            /* Nota Média de Avaliações */
            $acNotas = array(); //acumulador de notas geral
            foreach ($evals as $eval) {
                $sum = 0;
                $sum = $sum + $eval['strategy'];
                $sum = $sum + $eval['leadership'];
                $sum = $sum + $eval['culture'];
                $sum = $sum + $eval['opportunity'];
                $sum = $sum + $eval['recognition'];
                $sum = $sum + $eval['workspace'];
                $sum = $sum + $eval['payment'];
                $sum = $sum + $eval['flexibility'];
                $sum = $sum + $eval['workoverload'];
                $sum = $sum + $eval['coworkers'];
                $sum = $sum + $eval['salary'];
                $acNotas[] = $sum/11;
            }
            $companyItem['star_rate'] = round((array_sum($acNotas) / count($acNotas)), 2);
            
            /* Add to Companies  */
            $companies[] = $companyItem;
        }
        $this->view->companies = $companies;   
    }

    public function contatoAction()
    {
        // action body
    }

    public function faqAction()
    {
        // action body
    }

    public function termosAction()
    {
        // action body
    }

    public function privacidadeAction()
    {
        // action body
    }

    public function loginAction()
    {
        // action body
    }

    public function sendMailAction()
    {
        $settings = array('ssl'=>'ssl',
                        'port'=>465,
                        'auth' => 'login',
                        'username' => 'jobsel.messenger@gmail.com',
                        'password' => 'M3ns4g3ir0');
        $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $settings);
        
        $mail = new Zend_Mail('UTF-8');
        $mail->setReplyTo($_POST['email'], $_POST['name']);
        $mail->setFrom ($_POST['email'], $_POST['name']);
        $mail->addTo ("contato@jobsel.mobi", "Jobsel");
        $mail->setSubject ('Contato - por '.$_POST['name']);
        $mail->setBodyText ($_POST['message']);
        $mail->send($transport);
        $_SESSION["message_success"] = "Sua mensagem foi enviada.";
        $this->_helper->redirector('contato','index');
    }


}















